<?php


namespace App\Http\Service;


use App\Contract\ContractStatus;
use Carbon\Carbon;

class HouseService
{
    public function getHouseForList($house): array
    {

        $days = 0;
        $createDays = Carbon::now()->diffInDays($house->created_at);
        $failDays = 0;

        $status = $this->getHouseStatusName($house);
        if ($status['key'] == 'fail') {
            $bill = $house->contract->bills()->where('is_paid', 0)->where('is_close', 0)->where('pay_date', '<', now()->toDateString())
                ->orderBy('pay_date', 'asc')->first();
            if ($bill) {
                $failDays = Carbon::now()->diffInDays(Carbon::createFromFormat('Y-m-d', $bill->pay_date));
            }
        }
        if ($house->contract && $house->contract->expired_at) {
            $days = Carbon::now()->diffInDays(Carbon::createFromFormat('Y-m-d', $house->contract->expired_at));
        }

        return [
            'id' => $house->id,
            'name' => $house->estate_name . $house->name,
            'estate_name' =>  $house->estate_name,
            'house_name' =>  $house->name,
            'status' => $status['status'],
            'status_key' => $status['key'],
            'tenant_name' => $house->tenant_name,
            'tenant_mobile' => $house->tenant_mobile,
            'tenant_count' => $house->tenant_count,
            'dianfei' => $house->dianfei,
            'rent' => $house->rent,
            'contract_expired_at' => $house->contract_expired_at,
            'size' => $house->size,
            'deposit_num' => $house->deposit_num,
            'rent_num' => $house->rent_num,
            'rent_num_name' => $house->rent_num_name,
            'fang' => "{$house->fang}房{$house->ting}厅{$house->chu}厨{$house->wei}卫",
            'house_fang'=>$house->fang,
            'house_ting'=>$house->ting,
            'house_chu'=>$house->chu,
            'house_wei' => $house->wei,
            'area' => $house->province. $house->city . $house->district.$house->country,
            'created_at' => $house->created_at->toDateTimeString(),
            'days' => $days,
            'create_days' => $createDays,
            'fail_days' => $failDays,
            'device' => $house->device,
            'ttlock' =>$house->lock,
            'business' => $house->business ? $house->business->user : null,
            'contracts'=>$house->contract,
            'province'=>$house->province,
            'city'=>$house->city,
            'district'=>$house->district,
            'is_whole'=>$house->is_whole
        ];
    }

    public function getHouseStatusName($house)
    {
        $key = 'vacant';
        $status = '未出租';
        if ($house->contract) {
            if ($house->contract->status == ContractStatus::QIANDINGZHONG) {
                $status = '签订中';
                $key = 'signing';
                return [
                    'key' => $key,
                    'status' => $status
                ];
            }
            if ($house->contract->status == ContractStatus::DAIQIANYUE) {
                $status = '待签约';
                $key = 'signing';
                return [
                    'key' => $key,
                    'status' => $status
                ];
            }

            if ($house->contract->status == ContractStatus::FASTQY) {
                $status = '快捷入住';
                $key = 'fast';
                return [
                    'key' => $key,
                    'status' => $status
                ];
            }


            if ($house->contract->status == ContractStatus::YIQIANYUE && $house->contract->expired_at < now()->toDateString()) {
                $status = '租期已过';
                $key = 'expired';
                return [
                    'key' => $key,
                    'status' => $status
                ];
            }
            if ($house->contract->status == ContractStatus::YIQIANYUE && $house->contract->expired_at > now()->toDateString() && $house->contract->expired_at < now()->addDays(30)->toDateString()) {
                $status = '合同快到期';
                $key = 'expire';
                return [
                    'key' => $key,
                    'status' => $status
                ];
            }
            $failCount = $house->contract->bills()->where('is_paid', 0)->where('is_close', 0)->where('pay_date', '<', now()->toDateString())->count();
            if ($house->contract->status > ContractStatus::DAIQIANYUE && $failCount ) {
                $status = '账单逾期';
                $key = 'fail';
                return [
                    'key' => $key,
                    'status' => $status
                ];
            }
            if ($house->contract->status == ContractStatus::YIQIANYUE) {
                $status = '已出租';
                $key = 'hired';
                return [
                    'key' => $key,
                    'status' => $status
                ];
            }

        }
        return [
            'key' => $key,
            'status' => $status
        ];
    }
}
